package com.tbm.impl.controller;

import com.tbm.base.controller.BaseController;
import com.tbm.base.service.MenuService;
import com.tbm.common.util.JurisdictionUtil;
import com.tbm.common.util.PageData;
import com.tbm.entity.MenuEntity;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理员管理模块
 * 只做跳转，尽量不处理业务
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminManagementController extends BaseController {

    @Autowired
    private MenuService menuService;

    /**
     * 角色查询
     * @return
     */
    @RequestMapping("/admin-role-query")
    public ModelAndView adminRoleQuery(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "admin/admin-role-query";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    /**
     * 管理员
     * @return
     */
    @RequestMapping("/admin-list-manage")
    public ModelAndView adminListManage(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "admin/admin-list-manage";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    /**
     * 菜单管理
     * @return
     */
    @RequestMapping("/admin-menu-manage")
    public ModelAndView adminMenuManage(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            List<PageData> resultList = new ArrayList<PageData>();
            PageData pageData = this.getPageData();
            String id = pageData.getString("id")==null?"1234567890":pageData.getString("id");
            String flag = pageData.getString("isflag")==null||id.equals("-1")?"N":"Y";
            String parentName = "根目录";
            String parentId = "0";

            PageData rootPageData = new PageData();
            rootPageData.put("id",-1);
            rootPageData.put("pId","0");
            rootPageData.put("name","TBM菜单管理树");
            rootPageData.put("open",true);
            resultList.add(rootPageData);

            for(MenuEntity menuEntity : menuService.findAll()){
                PageData pd = new PageData();
                pd.put("id",menuEntity.getMenuId());
                pd.put("pId",menuEntity.getParentId().equals("0")?"-1":menuEntity.getParentId());
                pd.put("name",menuEntity.getMenuName());

                if(id.equals(String.valueOf(menuEntity.getMenuId()))){
                    parentName = menuEntity.getMenuName();
                    parentId = id;
                    pd.put("open",true);
                }else{
                    pd.put("open",false);
                }
                resultList.add(pd);
            }

            modelAndView.addObject("zNodes", JSONArray.fromObject(resultList));
            modelAndView.addObject("name",parentName);
            modelAndView.addObject("content","路由路径");
            modelAndView.addObject("icon","图标");
            modelAndView.addObject("rout","路径");
            modelAndView.addObject("isflag",flag);
            modelAndView.addObject("parent_id",parentId);
            viewName = "admin/admin-menu-manage";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    /**
     * 角色管理
     * @return
     */
    @RequestMapping("/admin-role-manage")
    public ModelAndView adminRoleManage(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "admin/admin-role-manage";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }


    /**
     * 角色分配界面
     * @return
     */
    @RequestMapping("/admin-role-assign")
    public ModelAndView adminRoleAssign(){
        ModelAndView modelAndView = this.getModelAndView();
        PageData pageData = this.getPageData();
        String rights = pageData.get("rights")==null?"1":pageData.get("rights").toString();
        int roleId = pageData.get("role_id")==null?-1:Integer.valueOf(pageData.get("role_id").toString());
        modelAndView.addObject("rights",rights);
        modelAndView.addObject("role_id",roleId);
        modelAndView.setViewName("admin/admin-role-assign");
        return modelAndView;
    }
}
